package com.ruoyi.sl.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.sl.service.ISlLevelService;
import com.ruoyi.sl.service.ISlModuleService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.sl.domain.SlApprentice;
import com.ruoyi.sl.service.ISlApprenticeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 学徒系数Controller
 * 
 * @author admin
 * @date 2024-06-03
 */
@RestController
@RequestMapping("/sl/apprentice")
public class SlApprenticeController extends BaseController
{
    @Autowired
    private ISlApprenticeService slApprenticeService;

    @Autowired
    private ISlModuleService slModuleService;

    @Autowired
    private ISlLevelService slLevelService;

    /**
     * 查询学徒系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:list')")
    @GetMapping("/list")
    public TableDataInfo list(SlApprentice slApprentice)
    {
        startPage();
        List<SlApprentice> list = slApprenticeService.selectSlApprenticeList(slApprentice);
        return getDataTable(list);
    }

    /**
     * 导出学徒系数列表
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:export')")
    @Log(title = "学徒系数", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SlApprentice slApprentice)
    {
        List<SlApprentice> list = slApprenticeService.selectSlApprenticeList(slApprentice);
        ExcelUtil<SlApprentice> util = new ExcelUtil<SlApprentice>(SlApprentice.class);
        util.exportExcel(response, list, "学徒系数数据");
    }

    /**
     * 获取学徒系数详细信息
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:query')")
    @GetMapping(value = { "/", "/{apprenticeId}" })
    public AjaxResult getInfo(@PathVariable(value = "apprenticeId", required = false) Long apprenticeId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("modules", slModuleService.selectModuleAll());
        ajax.put("levels", slLevelService.selectLevelAll());
        if (StringUtils.isNotNull(apprenticeId))
        {
            SlApprentice slApprentice = slApprenticeService.selectSlApprenticeByApprenticeId(apprenticeId);
            ajax.put(AjaxResult.DATA_TAG, slApprentice);
        }
        return ajax;
    }

    /**
     * 新增学徒系数
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:add')")
    @Log(title = "学徒系数", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SlApprentice slApprentice)
    {
        slApprentice.setCreateBy(getUsername());
        return toAjax(slApprenticeService.insertSlApprentice(slApprentice));
    }

    /**
     * 修改学徒系数
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:edit')")
    @Log(title = "学徒系数", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SlApprentice slApprentice)
    {
        slApprentice.setUpdateBy(getUsername());
        return toAjax(slApprenticeService.updateSlApprentice(slApprentice));
    }

    /**
     * 删除学徒系数
     */
    @PreAuthorize("@ss.hasPermi('sl:apprentice:remove')")
    @Log(title = "学徒系数", businessType = BusinessType.DELETE)
	@DeleteMapping("/{apprenticeIds}")
    public AjaxResult remove(@PathVariable Long[] apprenticeIds)
    {
        return toAjax(slApprenticeService.deleteSlApprenticeByApprenticeIds(apprenticeIds));
    }
}
